package com.ysstech.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.enums.YssEnum;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultCode;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.system.feign.FeignReportWorldService;
import com.ysstech.system.service.MenuService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
@Api(tags = "后台主页api")
@RequestMapping(value = "/main")
public class MainController extends BaseController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private FeignReportWorldService feignReportWorldService;

    /**
     * 主页的加载方法
     *
     * @return
     */
    @RequestMapping("/main")
    public String main(HttpServletRequest request) throws Exception {
        request.setAttribute("user", redisCacheManager.getLoginUserInfo());
        return "system/main";
    }

    /**
     * 初始化加载首页相关信息
     *
     * @return
     */
    @RequestMapping(value = "/initData")
    @ResponseBody
    public RestResult initData() throws Exception {
        // 统计当前报表世界标准数据的情况
        Map<String, Object> map = new HashMap<>();
        try {
            // 公告信息
            map.put("notice", "欢迎来到赢时胜报表世界标准数据管理平台！！！");
            // 网页底部信息
            map.put("footerMsg", YssEnum.FOOTER_MSG.getDesc());
            map.put("version", dicValueService.getMaxValue("REPORT_VERSION"));
            RestResult restResult = feignReportWorldService.statisticsReportWorld();
            // 返回成功
            if (ResultCode.SUCCESS.getCode() != restResult.getCode()) {
                return ResultGenerator.getSuccessResult("加载首页信息失败，请稍后重试！", map);
            }
            Map<String, Object> map2 = (Map<String, Object>) restResult.getData();
            if (CollectionUtils.isEmpty(map2)) {
                return ResultGenerator.getSuccessResult("没有获取到首页相关信息，请稍后重试！", map);
            }
            map.putAll(map2);
            return ResultGenerator.getSuccessResult("加载首页信息成功", map);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
        return ResultGenerator.getSuccessResult("加载首页信息成功", map);
    }


    /**
     * AJAX加载菜单信息 easyUi树
     *
     * @return
     */
    @RequestMapping(value = "/getUserMenu")
    @ResponseBody
    public RestResult getUserMenu() throws Exception {
        List<TreeNode> list = menuService.queryUserMenu(redisCacheManager.getLoginUserInfo());
        return ResultGenerator.getSuccessResult("获取菜单成功", list);
    }

}
